package com.wf.course.web.controller.course;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.wf.course.web.alioss.FileUploader;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.utils.page.PageBean;
import com.wf.course.model.course.CourseChapter;
import com.wf.course.model.course.CourseInfo;
import com.wf.course.service.ICourseChapterService;
import com.wf.course.service.ICourseInfoService;
import com.wf.course.web.PutObjectProgressListener;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 课程章节列表Controller
 */
@RequestMapping("courseChapter")
@Controller
public class CourseChapterController {

	@Autowired
	private ICourseChapterService chapterService;
	@Autowired
	private ICourseInfoService courseInfoService;

	/**
	 * 章节列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param courseId
	 * @param speakerName
	 * @param model
	 * @return
	 */
	@LogInfo("查询课程章节信息列表")
	@RequestMapping("chapterList")
	public String chapterList(HttpSession httpSession,
			@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "courseId", required = false) String courseId, 
			@RequestParam(value = "speakerName", required = false) String speakerName, 
			@RequestParam(value = "chapterType", required = false) String chapterType, 
			@RequestParam(value = "chapterStatus", required = false) String chapterStatus, 
			@RequestParam(value = "chapterName", required = false) String chapterName,
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<>();
		cons.put("courseId", courseId);
		cons.put("speakerName", speakerName);
		cons.put("chapterType", chapterType);
		cons.put("chapterStatus", chapterStatus);
		cons.put("chapterName", chapterName);
		PageBean<CourseChapter> pageBean = new PageBean<>(pageNo, pageSize, null, cons);
		pageBean.setUrl("courseChapter/chapterList");
		this.chapterService.indexInfo(pageBean);
		
		 List<CourseInfo> courses = courseInfoService.queryCourses();
		 model.addAttribute("courses",courses);
		
		model.addAttribute("page", pageBean);
		
		httpSession.setAttribute("page", pageBean);
		
		return "course/courseChapterList";
	}

	/**
	 * 课程 修改和新增页面数据初始化
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("新增或修改课程章节信息，页面数据初始化")
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model,HttpSession httpSession) {
		 
		if (id != null && id != 0) {
			CourseChapter courseChapter = chapterService.queryCourseChapterById(id);
			model.addAttribute("courseChapter", courseChapter);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		 List<CourseInfo> courses = courseInfoService.queryCourses();
		 model.addAttribute("courses",courses);
		 
		return "course/courseChapterEdit";
	}

	/**
	 * 课程新增和修改
	 * @param courseChapter
	 * @param imgFile
	 * @param videoFile
	 * @param request
	 * @return
	 */
	@LogInfo("新增或修改课程章节信息")
	@RequestMapping(value = "courseChapterEdit" , produces = "text/html;charset=UTF-8")
	public String courseChapterEdit(@ModelAttribute CourseChapter courseChapter, MultipartFile videoFile, 
			MultipartFile imgFile,HttpServletRequest request,
			HttpSession httpSession,Model model) {
		
		chapterService.saveOrUpdateCourseChapter(courseChapter, videoFile,imgFile);
		
		//保存后，跳转到之前的列表页
		@SuppressWarnings("unchecked")
		PageBean<T> page =  (PageBean<T>) httpSession.getAttribute("page");
		 
		PageBean<CourseChapter> pageBean = new PageBean<CourseChapter>(page.getActulPageNo(), page.getPageSize(), null, page.getConMap());
		pageBean.setUrl("courseChapter/chapterList");
		this.chapterService.indexInfo(pageBean);
		
		 List<CourseInfo> courses = courseInfoService.queryCourses();
		 model.addAttribute("courses",courses);
		
		model.addAttribute("page", pageBean);
		
		return "course/courseChapterList";
		
		
 
	}

	/**
	 * 课程删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除课程章节信息")
	@RequestMapping(value = "remove/{id}")
	public String removeCourseChapter(@PathVariable("id") Integer id) {
		
		chapterService.deleteById(id);
		
		return "redirect:/courseChapter/chapterList";
	}
	
	
	/**
	 * 上传视频页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("上传视频，页面初始化")
	@RequestMapping("uploadPage/{id}")
	public String uploadPage(@PathVariable("id") Integer id, Model model) {
		 
		CourseChapter courseChapter = chapterService.queryCourseChapterById(id);
		model.addAttribute("courseChapter", courseChapter);
 		
		return "course/courseChapterVideo";
	}
	
	/**
	 * 上传课程视频
	 */
	@LogInfo("添加/修改视频")
	@RequestMapping(value = "uploadVideo" , produces = "text/html;charset=UTF-8")
	public String uploadVideo(@ModelAttribute CourseChapter courseChapter,
							  MultipartFile videoFile, HttpServletRequest request) {
		long startTime=System.currentTimeMillis();

			 if(videoFile.getSize()!=0){
					chapterService.uploadVideo(courseChapter, videoFile);
				}
		//执行方法
		long endTime=System.currentTimeMillis();
		float excTime=(float)(endTime-startTime)/1000;
		System.out.println("执行时间："+excTime+"s");

		return "course/courseChapterVideo";
	}
	
	 /**
     * 上传进度查询
     * @return
     */
    @ResponseBody
	@RequestMapping(value="uploadPercent", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public String uploadPercent(MultipartFile videoFile){

		double percent = 0;
		try {
			percent = FileUploader.getPercent(videoFile);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return String.valueOf(percent);
    }

}
